package com.bea.ftd.dashboard.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.bea.ftd.common.ValidationException;
import com.bea.ftd.common.entity.MsgOutConf;
import com.bea.ftd.dashboard.domain.AjaxResponse;
import com.bea.ftd.dashboard.enums.ResponseStatus;
import com.bea.ftd.dashboard.service.IMsgOutConfService;

@RestController
@RequestMapping("/msgOutConf")
public class MsgOutConfController {
	private Logger LOGGER = LoggerFactory.getLogger(MsgOutConfController.class);

	@Autowired
	private IMsgOutConfService msgOutConfServiceImpl;

	@RequestMapping("/listAll")
	public List<MsgOutConf> listAllMsgOutConfs() {
		return msgOutConfServiceImpl.getAllMsgOutConfs();
	}

	@RequestMapping("/saveOrUpdate")
	public AjaxResponse addMsgOutConf(MsgOutConf msgOutConf) {
		LOGGER.info("Save or update msgOutConf - " + msgOutConf.toString());
		try {
			msgOutConfServiceImpl.addMsgOutConf(msgOutConf);
			return new AjaxResponse(ResponseStatus.SUCCESS);
		} catch (ValidationException e) {
			LOGGER.error("输入参数错误：" + e.getErrorMessage());
			return new AjaxResponse(ResponseStatus.FAIL, e.getErrorMessage());
		} catch (Exception e) {
			LOGGER.error("Error in saving msgOutConf to DB. " + e.getMessage());
			return new AjaxResponse(ResponseStatus.FAIL, e.getMessage());
		}

	}

	@RequestMapping("/addMsgOutConfFromExample")
	public AjaxResponse addMsgInConfFromExample(String exampleData, String apiId) {

		try {
			msgOutConfServiceImpl.addMsgInConfFromExample(apiId, exampleData);

		} catch (Exception e) {
			e.printStackTrace();
			return new AjaxResponse(ResponseStatus.FAIL, "报文解析错误");
		}
		return new AjaxResponse(ResponseStatus.SUCCESS);
	}
	
	@RequestMapping("/deleteMsgOutConfContent")
	public AjaxResponse deleteMsgOutConfContent(String apiId) {
		try {
			msgOutConfServiceImpl.deleteMsgOutConfContent(apiId);
		} catch (Exception e) {
			e.printStackTrace();
			return new AjaxResponse(ResponseStatus.FAIL, "报文解析错误");
		}
		return new AjaxResponse(ResponseStatus.SUCCESS);
	}

}
